﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc。@Copy Right 2019
// 文件： CarGroup.cs
// 项目名称：斑马车务
// 创建时间：2019-10-11
// 负责人：lsc
// ===================================================================
using System;
using System.Data;
using System.Collections.Generic;
using Dapper.Extensions;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Repository;
using YouWei.Infrastructure.Models;

namespace YouWei.Infrastructure.Service
{
	
	/// <summary>
	///群组
	/// </summary>
	public class CarGroupService
	{
        CarGroupBrandService ccbService = new CarGroupBrandService();
         protected CarGroupRepository repository;
         
         public  CarGroupService(){
           repository = new CarGroupRepository();         
         }
         
        #region 增删改查

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public object Insert(CarGroup entity) { return repository.Insert(entity); }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public object Insert(List<CarGroup> entitys) { return repository.Insert(entitys); }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Update(CarGroup entity) { return repository.Update(entity); }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Delete(CarGroup entity) { return repository.Delete(entity); }

        /// <summary>
        /// 删除所有
        /// </summary>
        /// <returns></returns>
        public bool DeleteAll() { return repository.DeleteAll(); }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public bool Delete(int primaryKey) { return repository.Delete(primaryKey); }

        /// <summary>
        /// 查询单个实体
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public CarGroup SingleOrDefault(object primaryKey) { return repository.SingleOrDefault(primaryKey); }

       
        /// <summary>
        /// 查询单个实体,如果存在多条则引发异常
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public CarGroup Single(object primaryKey) { return repository.Single(primaryKey); }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        public List<CarGroup> QueryAll()
        {
            return repository.QueryAll();
        }
        
        #endregion

        public Page<CarGroup> Pages(int pageSize,int pageIndex)
        {
            return repository.Pages(pageSize,pageIndex);
        }

        public Page<CarGroup> Pages(CarGroupSearchModel conditon)
        {
            return repository.Pages(conditon);
        }
       

        /// <summary>
        /// 分页查询，带条件
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public Page<CarGroupDto> GetPages(int pageIndex, long pageSize, ReqCarGroupCondition condition)
        {
            List<int> ids = new List<int>();
            Page<CarGroupDto> list = repository.GetPages(pageIndex, pageSize, condition);
            foreach (var item in list.rows)
            {
                ids.Add(item.CarGroupID);
            }
            Dictionary<int, List<CarGroupBrand>> dic = ccbService.GetCarGroupBrandDic(ids);

            foreach (var item in list.rows)
            {
                if (dic.ContainsKey(item.CarGroupID))
                {
                    item.BrandName = GetGroupCarInfoName(dic[item.CarGroupID], 1);
                    item.SerialName = GetGroupCarInfoName(dic[item.CarGroupID], 2);
                }
           
            }
            return list;
        }

        private string GetGroupCarInfoName(List<CarGroupBrand> list, int type)
        {
            string name = "";

            foreach (var li in list)
            {
                if (type == 1)
                {
                    name += li.BrandName + ",";
                }
                else if (type == 2)
                {
                    name += li.SerialName + ",";
                }

            }

            return name.TrimEnd(',');
        }

        public CarGroup GetCarGroup(int id)
        {
            return repository.GetCarGroup(id);
        }

        public List<CarGroup> GetCarGroups(string ids)
        {
            return repository.GetCarGroups(ids);
        }

        /// <summary>
        /// 查询群组详情
        /// </summary>
        /// <param name="carGroupId"></param>
        /// <returns></returns>
        public CarGroupDto GetInfo(int carGroupId)
        {
            return repository.GetInfo(carGroupId);
        }

        /// <summary>
        /// 查询群组详情
        /// </summary>
        /// <param name="carGroupId"></param>
        /// <returns></returns>
        public CarGroup GetGroupByModel(int ModelID)
        {
            return repository.GetGroupByModel(ModelID);
        }
        }
	
}